using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    public class DomainUpDown: IComponent
    {

        #region Variables
        protected Rectangle container = new Rectangle(0, 0, 100, 25);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush backColor = new SolidBrush(Color.Ivory);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected List<string> items;
        protected string name;
        protected string text;

        protected int selectedIndex;
        protected Rectangle up;
        protected Rectangle down;
        protected Point[] upPoints;
        protected Point[] downPoints;
        protected int buttonWidth;
        #endregion

        #region Properties
        /// <summary>
        /// Tells whether or not a component is hidden. If it is hidden,
        /// it should not be receiving actions.
        /// </summary>
        public bool Hidden
        {
            get { return hidden; }
        }

        /// <summary>
        /// Stores the width and height of the component
        /// </summary>
        public Size Size
        {
            get { return container.Size; }
            set { container.Size = value; Invalidate(); }
        }

        public Point Location
        {
            get { return container.Location; }
            set
            {
                container.Location = value;
                SetSubComponents();
                CreateArrows();
                Invalidate();
            }
        }

        public Rectangle Bounds
        {
            get { return container; }
            set
            {
                container = value;
                SetSubComponents();
                CreateArrows();
                Invalidate();
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return backColor.Color; }
            set { backColor.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion

        #region IComponent Members
        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                g.FillRectangle(backColor, container);
                g.FillRectangle(new SolidBrush(Color.PaleGoldenrod), up);
                Draw3DBorder(g, up);
                g.FillRectangle(new SolidBrush(Color.PaleGoldenrod), down);
                Draw3DBorder(g, down);

                
                float fontY = container.Y + font.Size / 2;
                float xMargin = 5;
                g.DrawString(items[selectedIndex], font, Brushes.Black, new PointF(container.X + xMargin, fontY));

                DrawArrows(g);
                Draw3DBorder(g, container);

                //Draw a box around the button to show that it has focus
                if (hasFocus)
                {
                    DrawFocusBorder(g);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            if (up.Contains(e.Location))
            {
                if (items.Count > 0)
                {
                    if (selectedIndex == 0)
                    {
                        selectedIndex = items.Count - 1;
                    }
                    else
                    {
                        selectedIndex--;
                    }
                    text = items[selectedIndex].ToString();
                    Invalidate();
                }
            }
            else if (down.Contains(e.Location))
            {
                if (items.Count > 0)
                {
                    if ((items.Count - 1) == selectedIndex)
                    {
                        selectedIndex = 0;
                    }
                    else
                    {
                        selectedIndex++;
                    }
                    text = items[selectedIndex].ToString();
                    Invalidate();
                }
            }
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }
        #endregion

        public List<string> Items
        {
            get
            {
                return items;
            }
            set
            {
                items = value;
            }
        }
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; }
        }
        public int ButtonWidth
        {
            get { return buttonWidth; }
            set { buttonWidth = value + (int)Pens.White.Width; }
        }

        public DomainUpDown()
        {
            items = new List<string>();
            buttonWidth = 15 + (int)Pens.White.Width;

            SetSubComponents();

            selectedIndex = 0;

            upPoints = new Point[3];
            downPoints = new Point[3];

            CreateArrows();
        }

        private void SetSubComponents()
        {
            up = new Rectangle(container.Right - buttonWidth, container.Top,
                    buttonWidth, container.Height / 2);
            down = new Rectangle(container.Right - buttonWidth, container.Bottom - container.Height / 2,
                buttonWidth, container.Height / 2);
        }

        private void DrawArrows(Graphics g)
        {
            Pen pen = new Pen(Brushes.DarkGoldenrod);
            g.DrawLine(pen, upPoints[0], upPoints[1] );
            g.DrawLine(pen, upPoints[1] , upPoints[2] );

            g.DrawLine(pen, downPoints[0] , downPoints[1] );
            g.DrawLine(pen, downPoints[1] , downPoints[2] );
        }

        private void CreateArrows()
        {
            int sideMargin = container.Width / 10;
            int vertMargin = container.Height / 10;

            upPoints[0] = new Point(up.Left + sideMargin, up.Bottom - vertMargin);
            upPoints[1] = new Point(up.Left + up.Width / 2, up.Top + vertMargin);
            upPoints[2] = new Point(up.Right - sideMargin, up.Bottom - container.Height / 10);

            downPoints[0] = new Point(down.Left + sideMargin, down.Top + vertMargin);
            downPoints[1] = new Point(down.Left + down.Width / 2, down.Bottom - vertMargin);
            downPoints[2] = new Point(down.Right - sideMargin, down.Top + vertMargin);
        }

        private void DrawFocusBorder(Graphics g)
        {
            int PenWidth = (int)Pens.White.Width;

            g.DrawLine(Pens.DarkGoldenrod, new Point(container.Left, container.Top),
                new Point(container.Right - PenWidth, container.Top));
            g.DrawLine(Pens.DarkGoldenrod, new Point(container.Left, container.Top),
                new Point(container.Left, container.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(container.Left, container.Bottom),
                new Point(container.Right - PenWidth, container.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(container.Right - PenWidth, container.Bottom),
                new Point(container.Right - PenWidth, container.Top));
        }

        private void Draw3DBorder(Graphics g, Rectangle r)
        {
            int PenWidth = (int)Pens.White.Width;

            g.DrawLine(Pens.PaleGoldenrod, new Point(r.Left, r.Top),
                new Point(r.Right - PenWidth, r.Top));
            g.DrawLine(Pens.PaleGoldenrod, new Point(r.Left, container.Top),
                new Point(r.Left, r.Bottom));
            g.DrawLine(Pens.DarkGoldenrod, new Point(r.Left, r.Bottom),
                new Point(r.Right - PenWidth, r.Bottom));
            g.DrawLine(Pens.DarkGoldenrod, new Point(container.Right - PenWidth, r.Bottom),
                new Point(r.Right - PenWidth, r.Top));
        }
    }
}
